/*
 * Project:     My Project Work, Copyright (C) 2011
 * Created at:  03.08.2011
 */
package de.myprojectwork.presenter;

import de.myprojectwork.common.util.Logger;
import de.myprojectwork.common.util.MyProjectWorkException;
import de.myprojectwork.model.DataDefinitionModel;
import de.myprojectwork.model.DataManipulationModel;
import de.myprojectwork.model.Table;
import de.myprojectwork.model.dto.Configuration;
import de.myprojectwork.view.DbToolView;
import de.myprojectwork.view.DbToolViewImpl.Action;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author Chris Holdschuh
 */
public class DbToolPresenterImpl extends AbstractPresenter implements DbToolPresenter {

    private DataDefinitionModel model;
    private DataManipulationModel modelDataMan;
    private DbToolView view;
    
    public DbToolPresenterImpl(DataDefinitionModel model) {
        this.model = model;
    }
    
    public DbToolPresenterImpl(DataDefinitionModel model, DataManipulationModel modelDataMan) {
        this.model = model;
        this.modelDataMan = modelDataMan;
    }
    
    public void setView(DbToolView view) {
        this.view = view;
    }

    @Override
    public void initUi() {
        view.initUi();
    }

    @Override
    public void executeAction(Action action) {
        view.postMessage("-- " + action.toString());
        try {
            switch (action) {
                // anzeigen
                case SHOW_TABLES:
                    List<String> tables = model.showTables();
                    for(String s : tables) {
                        view.postMessage(s);
                    }
                    break;
                
                // insert/select actions...
                case INSERT_TEST_DATA:
                    break;
                case SELECT_TEST_DATA:
                    List<Configuration> list = modelDataMan.get(Table.CONFIGURATION, Table.CONFIGURATION.clazz(), null);
                    for(Configuration c : list) {
                        view.postMessage(c.toString());
                    }
                    break;
                    
                // create actions...
                case CREATE_SEQUENCE:
                    model.createSequence();
                    break;
                case CREATE_ALL_TAB:
                    model.createTables();
                    break;
                case CREATE_TAB_ACTIVITY_CATEGORY:
                case CREATE_TAB_CONFIGURATION:
                case CREATE_TAB_HISTORY_ACTIVITY:
                case CREATE_TAB_TAG:
                case CREATE_TAB_LINK:
                case CREATE_TAB_PROJECT:
                case CREATE_TAB_RADAR:
                case CREATE_TAB_TODO:
                    model.createTable(action.getTable());
                    break;
                
                // delete actions...
                case DEL_ALL_ENTRIES:
                    for(Table table : Table.values()) {
                        model.deleteEntries(table);
                    }
                    break;
                case DEL_SEQUENCE:
                    model.deleteSequence();
                    break;
                case DEL_DB:
                    model.deleteDb();
                    break;
                case DEL_ALL_TAB:
                    model.deleteTables();
                    break;
                case DEL_TAB_ACTIVITY_CATEGORY:
                case DEL_TAB_CONFIGURATION:
                case DEL_TAB_HISTORY_ACTIVITY:
                case DEL_TAB_TAG:
                case DEL_TAB_LINK:
                case DEL_TAB_PROJECT:
                case DEL_TAB_RADAR:
                case DEL_TAB_TODO:
                    model.deleteTable(action.getTable());
                    break;
                
                default:
                    throw new MyProjectWorkException("Unbekannte Aktion!");
            }
            view.postMessage("\tOK");
            
        } catch (MyProjectWorkException | IOException ex) {
            Logger.error("Fehler beim Ausführen der Aktion '" + action + "'.", ex);
            view.postMessage("~~ ERROR ~~ Fehler beim Ausführen der Aktion '" + action + "'.");
            view.postMessage(ex.getMessage());
            if (ex.getCause() != null) {
                view.postMessage("\t" + ex.getCause().getMessage());
            }
            // Exception wird geschluckt... :-/
        } finally {
            view.postMessage(""); // new line
        }
    }

    @Override
    protected void update() {
        // nix zu zun
    }
    
}
